/************************************************************************/
/* Name : Kiwon Yun
/* Partner's name : Eric Rooney
/* File Name : Hand.cpp
/* The name of assignment : Iteration 2
/************************************************************************/

#include "Hand.h"

Hand::Hand(void)
{
}

Hand::~Hand(void)
{
	// Just clear vector containing cards, 
	// because pointers of cards on the hand were cleared already when the deck was deleted.
	m_vecCard.clear();
}

void Hand::AddCard( Card* a_pCard )
{
	m_vecCard.push_back( a_pCard );
}

void Hand::DeleteCard(bool m_bDlt1,bool m_bDlt2,bool m_bDlt3,bool m_bDlt4,bool m_bDlt5)
{
	vector<Card*> tempVec;
	Card* tempCard;
	// In next version, it won't be happened to have not cards, but it can be happened in this version.
	if( m_vecCard.size() == 0 )
	{
		cout << "This hand has not cards." << endl;
	}
	else
	{
		if(m_bDlt5==1)  
		{
			m_vecCard.pop_back();
		}
		else
		{
			tempCard=m_vecCard.back();
			tempVec.push_back(tempCard);
			m_vecCard.pop_back();
		}
		if(m_bDlt4==1)  
		{
			m_vecCard.pop_back();
		}
		else
		{
			tempCard=m_vecCard.back();
			tempVec.push_back(tempCard);
			m_vecCard.pop_back();
		}
		if(m_bDlt3==1)  
		{
			m_vecCard.pop_back();
		}
		else
		{
			tempCard=m_vecCard.back();
			tempVec.push_back(tempCard);
			m_vecCard.pop_back();
		}
		if(m_bDlt2==1)  
		{
			m_vecCard.pop_back();
		}
		else
		{
			tempCard=m_vecCard.back();
			tempVec.push_back(tempCard);
			m_vecCard.pop_back();
		}
		if(m_bDlt1==1)  
		{
			m_vecCard.pop_back();
		}
		else
		{
			tempCard=m_vecCard.back();
			tempVec.push_back(tempCard);
			m_vecCard.pop_back();
		}

		//swap the vectors
		tempVec.swap(m_vecCard);
	}
}

//Function to put cards in the correct order in hand after cards have been deleted
void Hand::ReorderCards(bool m_bDlt1,bool m_bDlt2,bool m_bDlt3,bool m_bDlt4,bool m_bDlt5)
{
	vector<Card*> tempVec;

	// In next version, it won't be happened to have not cards, but it can be happened in this version.
	if( m_vecCard.size() == 0 )
	{
		cout << "This hand has not cards." << endl;
	}
	else
	{
	  	if(m_bDlt1!=1)  
		{
			tempVec.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}	  
		if(m_bDlt2!=1)  
		{
			tempVec.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}
		if(m_bDlt3!=1)  
		{
			tempVec.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}
		if(m_bDlt4!=1)  
		{
			tempVec.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}	
		if(m_bDlt5!=1)  
		{
			tempVec.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}	
	}
	
	//swap the vectors
	tempVec.swap(m_vecCard);	
}

//Function to put cards in the correct order in hand after cards have been deleted
void Hand::FinalReorderCards(bool m_bDlt1,bool m_bDlt2,bool m_bDlt3,bool m_bDlt4,bool m_bDlt5)
{
	vector<Card*> tempVec1;
	vector<Card*> tempVec2;
	vector<Card*> tempVec3;
	vector<Card*> tempVec4;
	vector<Card*> tempVec5;
								
	// In next version, it won't be happened to have not cards, but it can be happened in this version.
	if( m_vecCard.size() == 0 )
	{
		cout << "This hand has not cards." << endl;
	}
	else
	{
	
	  	if(m_bDlt1==1)  
		{
			tempVec1.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}	  
		if(m_bDlt2==1)  
		{
			tempVec2.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}
		if(m_bDlt3==1)  
		{
			tempVec3.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}
		if(m_bDlt4==1)  
		{
			tempVec4.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}	
		if(m_bDlt5==1)  
		{
			tempVec5.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}	



	  	if(m_bDlt5!=1)  
		{
			tempVec5.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}	  
		if(m_bDlt4!=1)  
		{
			tempVec4.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}
		if(m_bDlt3!=1)  
		{
			tempVec3.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}
		if(m_bDlt2!=1)  
		{
			tempVec2.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}	
		if(m_bDlt1!=1)  
		{
			tempVec1.push_back(m_vecCard.back());
			m_vecCard.pop_back();
		}
		
		m_vecCard.push_back(tempVec1.back());
		m_vecCard.push_back(tempVec2.back());
		m_vecCard.push_back(tempVec3.back());
		m_vecCard.push_back(tempVec4.back());
		m_vecCard.push_back(tempVec5.back());
	}
}

void Hand::ShowHand()
{
	// In next version, it won't be happened to have not cards, but it can be happened in this version.
	if( m_vecCard.size() == 0 )
	{
		cout << "This hand has not cards." << endl;
	}
	else
	{
		int nIndex = 0;
		vector <Card*>::iterator it;
		for ( it = m_vecCard.begin(); it != m_vecCard.end( ) ; ++it )
		{
			Card* pCard;
			pCard = (*it);
			cout << "[" << nIndex + 1 << "] " << pCard->suit << " - " << pCard->value << endl;
			nIndex++;
		}
	}
}

Card* Hand::GetCard( int a_nIndex )
{
	if( a_nIndex >= 0 && a_nIndex < (int)m_vecCard.size() )
		return m_vecCard[ a_nIndex ];
	else
		return NULL;
}

void Hand::ClearHand()
{
	vector <Card*>::iterator it;
	for ( it = m_vecCard.begin(); it != m_vecCard.end( ) ; ++it )
	{
		Card* pCard = (*it);

		if( pCard != NULL )
		{
			delete pCard;
			pCard = NULL;
		}
	}
	m_vecCard.clear();
}
